<script setup lang="ts">
import {computed, ref} from 'vue'
import Cookies from "js-cookie";

interface ApiSettings {
  api?: string;
  params?: {
    [key: string]: any;
  };
  config?: {
    [key: string]: any;
  };
}

const token = ref(Cookies.get('token') || Cookies.get('access_token'));

const props = withDefaults(defineProps<{
  commonApiSettings?: ApiSettings;
  statisticsApiSettings?: ApiSettings;
  toolsListApiSettings?: ApiSettings;
}>(), {
  commonApiSettings: () => ({
    api: 'https://www.china9.cn/api/app/aiToolOften',
  }),
  statisticsApiSettings: () => ({
    api: 'https://www.china9.cn/api/app/aiToolClick'
  }),
  toolsListApiSettings: () => ({
    api: 'https://www.china9.cn/api/app/getAItoolData'
  }),
})

const apiConfig = computed(() => {
  return {
    common: {
      ...props.commonApiSettings.config,
      headers: {
        'Content-Type': 'application/json',
        'token': token.value
      }
    },
    statistics: {
      ...props.statisticsApiSettings.config,
      headers: {
        'Content-Type': 'application/json',
        'token': token.value
      }
    },
    tools: {
      ...props.toolsListApiSettings.config,
      headers: {
        'Content-Type': 'application/json',
        'token': token.value
      }
    }
  }
})

// 获取常用工具
const commonTools = ref<any[]>([])
const getCommonTools = async () => {
  if (props.commonApiSettings.api != null) {
    try {
      const response = await fetch(props.commonApiSettings.api, {
        method: 'POST',
        headers: apiConfig.value.common.headers,
        body: JSON.stringify(props.commonApiSettings.params)
      });
      const res = await response.json();
      if (response.status === 200 && res.code === 200 && res.data) {
        commonTools.value = res.data;
      } else {
        console.log('请求失败');
      }
    } catch (error) {
      console.log('请求失败', error);
    }
  }
}
getCommonTools();

// 跳转和统计
const jump = async (item: any) => {
  let link = item.url
  if (!link.includes('http://') && !link.includes('https://')) {
    link = 'http://' + link;
  }
  window.open(link, '_blank');
  if (props.statisticsApiSettings.api != null) {
    try {
      const response = await fetch(props.statisticsApiSettings.api, {
        method: 'POST',
        headers: apiConfig.value.statistics.headers,
        body: JSON.stringify(props.statisticsApiSettings.params)
      });
      if (response.status === 200) {
        getCommonTools();
      } else {
        console.log('请求失败');
      }
    } catch (error) {
      console.log('请求失败', error);
    }
  }
}

// 获取工具列表
const aiTools = ref<any[]>([])
const getAiTools = async () => {
  if (props.toolsListApiSettings.api != null) {
    try {
      const response = await fetch(props.toolsListApiSettings.api, {
        method: 'POST',
        headers: apiConfig.value.tools.headers,
        body: JSON.stringify(props.toolsListApiSettings.params)
      });
      const res = await response.json();
      if (response.status === 200 && res.code === 200 && res.data) {
        aiTools.value = res.data;
      } else {
        console.log('请求失败');
      }
    } catch (error) {
      console.log('请求失败', error);
    }
  }
}
getAiTools();
</script>